import i18n from '@src/locales';
import { getPointsAndServiceFeeEnable, getAllCardType, getMemberConfig } from '@src/api/Member'

const memberAllFields = [{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'cardNo',
  displayName: i18n.t('common.member.memberNo'),
  formType: 'text',
  defaultValue: null,
  isNull: 1, // 是否开启校验
  isSearch: 1,
  setting: {},
  maxlength: 20,
  orderId: 0, // 排序
  isDelete: 0,
  placeHolder: i18n.t('member.components.title2'),
  isEdit: 1, // 是否在编辑页显示
  show: true, // 是否在列表页显示
  export: true, // 是否可以导出
  guideProfessions: [],
  isGuideData: false,
  guideData: false,
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'cardTypeId',
  displayName: i18n.t('common.member.cardType'),
  formType: 'select',
  defaultValue: null,
  isNull: 0, // 是否开启校验
  isSearch: 1,
  setting: {
    isMultiFlag: true,
    isMulti: true,
    dataSource: []
  },
  orderId: 0, // 排序
  isDelete: 0,
  isEdit: 1, // 是否在编辑页显示
  show: true, // 是否在列表页显示
  export: true, // 是否可以导出
  guideProfessions: [],
  isGuideData: false,
  guideData: false,
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'customer',
  displayName: i18n.t('common.fields.customerName.displayName'),
  formType: 'customer',
  defaultValue: null,
  isNull: 1, 
  isSearch: 1,
  setting: {},
  orderId: 1, 
  isDelete: 0,
  isEdit: 1, 
  show: true, 
  export: true, 
  placeHolder: i18n.t('common.placeholder.selectCustomer'),
}, {
  tableName: 'member',
  isSystem: 1,
  fieldName: 'customerSerialnumber',
  displayName: i18n.t('customer.customerNo'),
  formType: 'text',
  defaultValue: null,
  isNull: 1, 
  isSearch: 1,
  setting: {},
  orderId: 2, 
  isDelete: 0,
  isEdit: 1, 
  show: true, 
  export: true, 
  placeHolder: i18n.t('common.placeholder.input2'),
  disabled: 1,
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'mainLinkmanPhone',
  displayName: i18n.t('common.member.principalLicensor'),
  formType: 'customer',
  defaultValue: null,
  isNull: 1, 
  isSearch: 0,
  setting: {},
  orderId: 3, 
  isDelete: 0,
  isEdit: 1, 
  show: true, 
  export: true, 
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'points',
  displayName: i18n.t('common.member.point'),
  formType: 'number-range',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  setting: {},
  maxlength: 50,
  orderId: 4,
  isDelete: 0,
  isEdit: 1,
  show: true,
  export: true,
  placeHolder: i18n.t('common.placeholder.input2')
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'pointsEffectTime',
  displayName: i18n.t('common.member.pointValidityPeriod'),
  formType: 'datetime',
  defaultValue: null,
  isNull: 0,
  isSearch: 0,
  setting: {},
  maxlength: 50,
  orderId: 5,
  isDelete: 0,
  isEdit: 1,
  show: false,
  export: false,
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'balance',
  displayName: i18n.t('common.member.balance'),
  formType: 'number-range',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  setting: {},
  maxlength: 50,
  orderId: 6,
  isDelete: 0,
  isEdit: 1,
  show: true,
  export: true,
  placeHolder: i18n.t('common.placeholder.input2')
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'serviceFee',
  displayName: i18n.t('common.member.serviceFee'),
  formType: 'number-range',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  setting: {},
  maxlength: 50,
  orderId: 7,
  isDelete: 0,
  isEdit: 1,
  show: true,
  export: true,
  placeHolder: i18n.t('common.placeholder.input2')
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'serviceFeeEffectTime',
  displayName: i18n.t('common.member.serviceFeeValidityPeriod'),
  formType: 'datetime',
  defaultValue: null,
  isNull: 0,
  isSearch: 0,
  setting: {},
  maxlength: 50,
  orderId: 8,
  isDelete: 0,
  isEdit: 1,
  show: false,
  export: false,
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'authorizationLimit',
  displayName: i18n.t('common.member.authorizationLimit'),
  formType: 'number-range',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  setting: {},
  maxlength: 50,
  orderId: 9,
  isDelete: 0,
  isEdit: 1,
  show: true,
  export: true,
  placeHolder: i18n.t('common.placeholder.input2')
}, {
  tableName: 'member',
  isSystem: 1,
  fieldName: 'limitEffectTime',
  displayName: i18n.t('common.member.limitPeriod'),
  formType: 'date',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  setting: {},
  maxlength: 50,
  orderId: 10,
  isDelete: 0,
  isEdit: 1,
  show: true,
  export: true,
}, 
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'createTime',
  displayName: i18n.t('common.base.createTime'),
  formType: 'datetime',
  defaultValue: null,
  isNull: 0,
  isSearch: 0,
  setting: {},
  maxlength: 50,
  orderId: 12,
  isDelete: 0,
  isEdit: 0,
  show: true,
  export: true,
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'enabled',
  displayName: i18n.t('common.member.enabled'),
  formType: 'select',
  defaultValue: '',
  isNull: 1, // 是否开启校验
  isSearch: 1,
  setting: {},
  orderId: 99, // 排序
  isDelete: 0,
  isEdit: 1, // 是否在编辑页显示
  show: true, // 是否在列表页显示
  export: true, // 是否可以导出
  guideProfessions: [],
  isGuideData: false,
  guideData: false,
  setting: {
    isMulti: false, 
    dataSource: [
      {
        text: i18n.t('common.base.all'),
        value: ''
      },
      {
        text: i18n.t('common.base.enable'),
        value: 1
      },
      {
        text: i18n.t('common.base.disable'),
        value: 0
      }
    ]
  }
},
{
  tableName: 'member',
  isSystem: 1,
  fieldName: 'endTime',
  displayName: i18n.t('common.member.endTime'),
  formType: 'datetime',
  defaultValue: '',
  isNull: 1, // 是否开启校验
  isSearch: 0,
  setting: {},
  orderId: 99, // 排序
  isDelete: 0,
  isEdit: 1, // 是否在编辑页显示
  show: true, // 是否在列表页显示
  export: true, // 是否可以导出
  guideProfessions: [],
  isGuideData: false,
  guideData: false,
},
]

const memberUsageRecordAllFields = [{
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'cardNo',
  displayName: i18n.t('common.member.memberNo'),
  formType: 'text',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('member.components.title2'),
  setting: {},
  show: true,
  export: true,
  orderId: 0,
  isDelete: 0,
  isEdit: 1,
  disabled: 1,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'customer',
  displayName: i18n.t('common.fields.customerName.displayName'),
  formType: 'customer',
  defaultValue: '',
  isNull: 0,
  isSearch: 1,
  isHidden: 0,
  isVisible: true,
  setting: {
    customerOption: {
      address: false,
      linkman: false,
      product: false,
    }
  },
  placeHolder: '',
  subFormFieldList: null,
  orderId: 1,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'module',
  displayName: i18n.t('bill.list.displayName.businessTypeName'),
  formType: 'select',
  defaultValue: null,
  isNull: 0, // 是否开启校验
  isSearch: 1,
  operator: 'eq',
  setting: {
    isMulti: false,
    dataSource: [
      // {
      //   text: i18n.t('common.base.task'),
      //   value: 'task',
      // }, 
      {
        text: i18n.t('common.member.portalMall'),
        value: 'shop',
      }, 
      // {
      //   text: 'PaaS',
      //   value: 'PaaS',
      // }
    ],
  },
  orderId: 2,
  isDelete: 0,
  show: true, // 是否在列表页显示
  export: true, // 是否可以导出
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'objNo',
  displayName: i18n.t('common.fields.businessLabel.displayName'),
  formType: 'text',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('member.list.title8'),
  setting: {},
  show: true,
  export: true,
  orderId: 3,
  isDelete: 0,
  isEdit: 1,
  disabled: 1,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'useIntegral',
  displayName: i18n.t('member.fields.title14'),
  formType: 'number-range',
  defaultValue: {},
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('common.placeholder.inputNumber'),
  setting: {},
  orderId: 4,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'useBalance',
  displayName: i18n.t('member.fields.title15'),
  formType: 'number-range',
  defaultValue: {},
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('common.placeholder.inputNumber'),
  setting: {},
  orderId: 5,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'useServiceFee',
  displayName: i18n.t('member.fields.title16'),
  formType: 'number-range',
  defaultValue: {},
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('common.placeholder.inputNumber'),
  setting: {},
  orderId: 6,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'useLines',
  displayName: i18n.t('member.fields.title17'),
  formType: 'number-range',
  defaultValue: {},
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('common.placeholder.inputNumber'),
  setting: {},
  orderId: 7,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'paymentAccount',
  displayName: i18n.t('task.detail.components.payAccount'),
  formType: 'text',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  placeHolder: i18n.t('member.list.title9'),
  setting: {},
  show: true,
  export: true,
  orderId: 8,
  isDelete: 0,
  isEdit: 1,
  disabled: 1,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'tradeCreateTime',
  displayName: i18n.t('task.detail.components.payCreateTime'),
  formType: 'datetime',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  placeHolder: null,
  setting: {},
  orderId: 9,
  isDelete: 0,
  show: true,
  export: true,
}, {
  tableName: 'memberUsageRecord',
  isSystem: 1,
  fieldName: 'paymentTime',
  displayName: i18n.t('bill.list.displayName.payTime'),
  formType: 'datetime',
  defaultValue: null,
  isNull: 0,
  isSearch: 1,
  placeHolder: null,
  setting: {},
  orderId: 10,
  isDelete: 0,
  show: true,
  export: true,
}]

const exchangeRuleFields = [
  {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'objectNo',
    displayName: i18n.t('common.base.serialNumber'),
    formType: 'text',
    defaultValue: null,
    isNull: 0,
    isSearch: 0,
    placeHolder: i18n.t('member.list.title10'),
    setting: {},
    show: true,
    export: true,
    orderId: 0,
    isDelete: 0,
    isEdit: 1,
    disabled: 1,
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'objectName',
    displayName: i18n.t('common.base.name'),
    formType: 'text',
    defaultValue: null,
    isNull: 0,
    isSearch: 0,
    placeHolder: i18n.t('common.placeholder.inputName'),
    setting: {},
    maxlength: 50,
    orderId: 1,
    isDelete: 0,
    show: true,
    export: true,
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'objectAttachment',
    displayName: i18n.t('order.productImg'),
    formType: 'attachment',
    defaultValue: null,
    isNull: 0,
    isSearch: 0,
    placeHolder: null,
    setting: {},
    orderId: 1,
    isDelete: 0,
    isEdit: 1,
    show: true,
    export: false,
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'objectType',
    displayName: i18n.t('common.base.type'),
    formType: 'select',
    defaultValue: null,
    isNull: 0, // 是否开启校验
    isSearch: 1,
    operator: 'eq',
    setting: {
      isMulti: false,
      dataSource: [{
        text: i18n.t('common.base.sparePart'),
        value: 1,
      }, {
        text: i18n.t('common.form.type.material'),
        value: 3,
      }, {
        text: i18n.t('common.label.serviceItem'),
        value: 2,
      }],
    },
    orderId: 2,
    isDelete: 0,
    show: true, // 是否在列表页显示
    export: true, // 是否可以导出
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'objectPrice',
    displayName: i18n.t('goods.component.price'),
    formType: 'number',
    defaultValue: null,
    isNull: 0,
    isSearch: 0,
    placeHolder: null,
    setting: {
      limitConig: {
        isLimit: 0,
        type: 1,
        min: 0.01,
        max: 99999
      }
    },
    orderId: 3,
    isDelete: 0,
    show: true,
    export: true,
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'exchangeType',
    displayName: i18n.t('common.member.exchangeType'),
    formType: 'select',
    defaultValue: null,
    isNull: 0,
    isSearch: 1,
    operator: 'eq',
    setting: {
      isMulti: false,
      dataSource: [{
        text: i18n.t('member.list.title11'),
        value: 1,
      }, {
        text: i18n.t('member.list.title12'),
        value: 2,
      }, {
        text: i18n.t('member.list.title15'),
        value: 3,
      }, {
        text: i18n.t('member.list.title16'),
        value: 4,
      }, {
        text: i18n.t('member.list.title13'),
        value: 5,
      }, {
        text: i18n.t('member.list.title14'),
        value: 6,
      }],
    },
    orderId: 4,
    isDelete: 0,
    show: true, // 是否在列表页显示
    export: true, // 是否可以导出
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'ruleDesc',
    displayName:  i18n.t('common.member.exchangeRules'),
    formType: 'select',
    defaultValue: null,
    isNull: 0,
    isSearch: 1,
    operator: 'eq',
    setting: {},
    orderId: 4,
    isDelete: 0,
    show: true, // 是否在列表页显示
    export: true, // 是否可以导出
  }, {
    tableName: 'exchangeRule',
    isSystem: 1,
    fieldName: 'updateTime',
    displayName: i18n.t('common.base.column.updateTime'),
    formType: 'datetime',
    defaultValue: null,
    isNull: 0,
    isSearch: 0,
    placeHolder: null,
    setting: {},
    orderId: 5,
    isDelete: 0,
    show: true,
    export: true,
  },
]

const formatFields = (configFields) => {
  let obj = {};

  const Fields = configFields.map(field => {
    return Object.assign({
      ...field,
      isNull: 1,
      formType: field.formType,
      originalFormType: field.formType,
    });
  }).sort((a, b) => a.orderId - b.orderId)
    .reduce((item, next) => {
      // 过滤掉相同fieldName的字段 去重
      obj[next.fieldName] ? '' : obj[next.fieldName] = true && item.push(next);
      return item;
    }, []);

  return Fields;
}

const pointRechargeFields = [
  {
    label: i18n.t('member.detail.title6'),
    field: 'createTime',
    tooltip: true,
  }, 
  {
    label: i18n.t('common.member.pointValidityPeriod'),  
    field: 'validityPeriod',
    tooltip: true,
    minWidth: '150px'
  }, 
  {
    label: i18n.t('member.components.title40'), 
    field: 'availableNum',
    tooltip: true,
  }, 
  {
    label: '操作', 
    field: 'action',
    tooltip: false,
    minWidth: '80px',
  }
]

const serviceFeeRechargeFields = [
  {
    label: i18n.t('member.detail.title6'),
    field: 'createTime',
    tooltip: true,
  }, 
  {
    label: i18n.t('common.member.serviceFeeValidityPeriod'), 
    field: 'validityPeriod',
    tooltip: true,
    minWidth: '150px'
  }, 
  {
    label: i18n.t('member.components.title42'), 
    field: 'availableNum',
    tooltip: true,
  }, 
  {
    label: i18n.t('common.base.operation'), 
    field: 'action',
    tooltip: false,
    minWidth: '80px',
  }
]
const authoriseColumns = [
  {
    label: i18n.t('member.list.title20'), 
    field: 'main',
    tooltip: true,
  }, 
  {
    label: i18n.t('member.list.title21'), 
    field: 'linkmanName',
    tooltip: true,
  },
  {
    label: i18n.t('member.list.title17'), 
    field: 'linkmanPhone',
    tooltip: true,
  }, 
  {
    label: i18n.t('member.list.title18'), 
    field: 'createTime',
    tooltip: true,
  },
  {
    label: i18n.t('common.base.operation'),  
    field: 'action',
    tooltip: false,
    minWidth: '80px',
  }
]

// 根据开关按钮隐藏字段
const getMemberFields = async () => {
  let memberFields = memberAllFields;
  let memberUsageRecordFields = memberUsageRecordAllFields;

  try {
    const { success, result: CardTypeList } = await getAllCardType({})
    if (success) {
      memberAllFields.map(v => {
        if (v.fieldName === 'cardTypeId') {
          v.setting.dataSource = CardTypeList.map(item => {
            item.value = item.id
            item.text = item.membershipTypeName
            return item
          })
        }
      })
    }

    const { status, data } = await getPointsAndServiceFeeEnable({})

    if (status === 0) {
      memberFields = memberAllFields.filter(v => {
        if (!data?.pointsEnable && ['points', 'pointsEffectTime'].includes(v.fieldName)) return false;
        if (!data?.serviceFeeEnable && ['serviceFee', 'serviceFeeEffectTime'].includes(v.fieldName)) return false;
        if (!data?.balanceEnable && ['balance'].includes(v.fieldName)) return false;
        if (!data?.quotaEnable && ['authorizationLimit', 'limitEffectTime'].includes(v.fieldName)) return false;

        return true;
      })

      memberUsageRecordFields = memberUsageRecordAllFields.filter(v => {
        if (!data?.pointsEnable && v.fieldName === 'useIntegral') return false;
        if (!data?.serviceFeeEnable && v.fieldName === 'useServiceFee') return false;
        if (!data?.balanceEnable && v.fieldName === 'useBalance') return false;
        if (!data?.quotaEnable && v.fieldName === 'useLines') return false;

        return true;
      })
    }

    const res = await getMemberConfig()
    if (!res?.data?.multiMembershipCard) {
      memberFields = memberAllFields.filter(v => v.fieldName != 'cardTypeId')
    }
  } catch (error) {
    console.error('getMemberAuth', error)
  }

  return {
    memberFields,
    memberUsageRecordFields,
  }
}

export {
  getMemberFields,
  formatFields,
  exchangeRuleFields,
  pointRechargeFields,
  serviceFeeRechargeFields,
  authoriseColumns,
}
